using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Threading;
using SimpleUtils;
using SimpleUtils.Win;
using SimpleReminder.Properties;
using System.Security.Cryptography;

namespace SimpleReminder
{
    internal class Utils
    {
        public static void InitReminderComboBox(ComboBox.ObjectCollection items)
        {
            items.Add(MyEvents.REMINDER_AGENT);
            items.Add(MyEvents.REMINDER_EMAIL);
            items.Add(MyEvents.REMINDER_APP);
        }

        public static void InitStatusComboBox(ComboBox.ObjectCollection items)
        {
            items.Add(MyEvents.EventStatus.Active.ToString());
            items.Add(MyEvents.EventStatus.Completed.ToString());
            items.Add(MyEvents.EventStatus.Disabled.ToString());
        }

        public static void InitTypeComboBox(ComboBox cbo)
        {
            cbo.Items.Add(MyEvents.EventTypes.Secondly.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Minutely.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Hourly.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Daily.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Monthly.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Weekly.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Yearly.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Once.ToString());
            cbo.Items.Add(MyEvents.EventTypes.Startup.ToString());
            cbo.SelectedIndex = 0;
        }


        public static void CheckForUpdate(bool silent)
        {
            SimpleUpdater.CheckForUpdate("https://sites.google.com/site/simpledotnet/simple-reminder/version",
                "https://sites.google.com/site/simpledotnet/simple-reminder",
                silent);
        }

        public static void ShowAbout()
        {
            SimpleMessage.ShowAbout("This FREE tool is provided \"AS IS\" with no warranties,\r\nand confers no rights.\r\n" +
                "Use it at your own risk.\r\n\r\n" +
                "Credits:\r\nMicrosoft Agent\r\n"
                );
        }

        public static MyEvents.EventTypes ParseEventType(string type)
        {
            return (MyEvents.EventTypes)Enum.Parse(typeof(MyEvents.EventTypes), type);            
        }

        public static MyEvents.EventStatus ParseEventStatus(string status)
        {
            return (MyEvents.EventStatus)Enum.Parse(typeof(MyEvents.EventStatus), status);
        }

        public static string GetString(DateTime dt)
        {
            return String.Format("{0:yyyy-MM-dd}", dt);
        }

        public static string GetString(TimeSpan ts)
        {
            return String.Format("{0:HH:mm}", ts);
        }

        public static int GetWeekNo(DateTime dt)
        {
            CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            Calendar cal = ci.Calendar;
            return cal.GetWeekOfYear(dt,
                ci.DateTimeFormat.CalendarWeekRule,
                ci.DateTimeFormat.FirstDayOfWeek);
        }

        public static bool SendEmail(string to, string cc, string bcc, string subject, string body)
        {
            if (String.IsNullOrEmpty(Settings.Default.SmtpServer) ||
                String.IsNullOrEmpty(Settings.Default.SmtpSenderName) ||
                String.IsNullOrEmpty(Settings.Default.SmtpSenderEmail))
            {
                SimpleMessage.ShowInfo("Please set SMTP options.");
                return false;
            }

            SimpleMail m = new SimpleMail(Settings.Default.SmtpServer, 
                Settings.Default.SmtpSenderEmail, 
                Settings.Default.SmtpSenderName);
            m.UserName = Settings.Default.SmtpUserName;
            m.Password = Settings.Default.SmtpPassword;
            m.Send(to, null, subject,
                (String.IsNullOrEmpty(body) ? "" : body.Replace("\n", "<br/>")),
                cc, bcc, null);
            return true;
        }

        private static RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider();
        public static int GetRandomNo(int max)
        {
            byte[] randomBytes = new Byte[4];
            _rng.GetNonZeroBytes(randomBytes);
            int randomSeed = (randomBytes[0] << 24) | (randomBytes[1] << 16) | (randomBytes[2] << 8) | randomBytes[3];
            int var = randomSeed % max;
            if (var < 0) var *= -1;
            return var;

            //Random r = new Random();
            //return r.Next(max);
        }

        /*
        public static void CompactDatabase()
        {
            object[] oParams;
            string dbFile = Db.DbPath;
            string tempFile = Path.Combine(Path.GetTempPath(), "tmp_" + Path.GetFileName(dbFile));

            try
            {
                //create an inctance of a Jet Replication Object
                object objJRO =
                    Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));

                //filling Parameters array
                //cnahge "Jet OLEDB:Engine Type=5" to an appropriate value
                // or leave it as is if you db is JET4X format (access 2000,2002)
                //(yes, jetengine5 is for JET4X, no misprint here)
                oParams = new object[] {    
                    Db.ConnectionString,
					String.Format("Provider=Microsoft.Jet.OLEDB.4.0;;Jet OLEDB:Engine Type=5;Data Source={0}", tempFile)
                };

                //invoke a CompactDatabase method of a JRO object
                //pass Parameters array
                objJRO.GetType().InvokeMember("CompactDatabase",
                    System.Reflection.BindingFlags.InvokeMethod,
                    null,
                    objJRO,
                    oParams);

                //database is compacted now
                //to a new file 
                //let's copy it over an old one and delete it

                File.Delete(dbFile);
                File.Move(tempFile, dbFile);

                //clean up (just in case)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
                objJRO = null;
            }
            catch
            {
                throw;
            }
            finally
            {
            }
        }
        */   
    }//end of class
}
